package com.smalls.newvideotwo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ProgressBar;

import com.smalls.newvideotwo.utils.MeasureUtil;
import com.smalls.newvideotwo.R;

public class LoadProgressBarView extends ProgressBar {

	private boolean init = false;
	private boolean load = false;
	private int width = 0;
	private int height = 0;
	public LoadProgressBarView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public LoadProgressBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public LoadProgressBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context,attrs);
		// TODO Auto-generated constructor stub
	}
	
	private void init(Context context, AttributeSet attrs) {
		if (!init) {
			init = true;
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Views);
			width = a.getLayoutDimension(R.styleable.Views_android_layout_width, 0);
			height = a.getLayoutDimension(R.styleable.Views_android_layout_height, 0);
			a.recycle();
			width = (int) MeasureUtil.getWidthSize(width);
			height = (int) MeasureUtil.getHeightSize(height);
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (!load) {
			load = true;
			LayoutParams params = getLayoutParams();
			if (params != null && params instanceof MarginLayoutParams) {
				fillLayoutParams((MarginLayoutParams) params);
				if (width > 0) {
					params.width = width;
				}
				if (height > 0) {
					params.height = height;
				}
			}
			setLayoutParams(params);
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private void fillLayoutParams(MarginLayoutParams params) {
		params.bottomMargin = (int) MeasureUtil
				.getHeightSize(params.bottomMargin);
		params.topMargin = (int) MeasureUtil.getHeightSize(params.topMargin);
		params.leftMargin = (int) MeasureUtil.getWidthSize(params.leftMargin);
		params.rightMargin = (int) MeasureUtil.getWidthSize(params.rightMargin);
	}



}
